package com.coollang.smartbra.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.coollang.smartbra.R;
import com.coollang.smartbra.adapter.NewCountActionDetailsAdapter;
import com.coollang.smartbra.beans.NewActionData;
import com.coollang.smartbra.constant.Constants;
import com.coollang.smartbra.utils.TranslucentSystemBarUtils;

import java.util.ArrayList;
import java.util.Collections;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

/**
 * @author: Timmy
 * @data: 2017/12/15
 * @Fuction:
 */
public class NewCountActionDetailsActivity extends AppCompatActivity {

    @BindView(R.id.status_bar)
    View mStatusBar;
    @BindView(R.id.content)
    LinearLayout content;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_second)
    TextView mTvSecond;
    @BindView(R.id.tv_error_action_count)
    TextView mTvErrorActionCount;
    @BindView(R.id.pb_progressbar)
    ProgressBar mProgressBar;
    @BindView(R.id.tv_correct_proportion)
    TextView mTvCorrectProportion;
    @BindView(R.id.tv_error_proportion)
    TextView mTvErrorProportion;
    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    @BindView(R.id.iv_back)
    ImageView mIvBack;
    private ArrayList<NewActionData> mActionDatas;
    private NewCountActionDetailsAdapter mDetailsAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_count_action_details);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        TranslucentSystemBarUtils.showChenJinZhuangTaiLan(mStatusBar, this, content);

        processIntent();
        initView();
        initData();
    }

    private void processIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            mActionDatas = getIntent().getParcelableArrayListExtra(Constants.LIST);
            Collections.reverse(mActionDatas);
        }
    }

    private void initView() {
        mIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        tvTitle.setText("动作详情");
        tvTitle.setTextColor(ContextCompat.getColor(this, R.color.black_666));

        mDetailsAdapter = new NewCountActionDetailsAdapter(null);
        // 取消RecycleView的动画
        mDetailsAdapter.isFirstOnly(false);

        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mDetailsAdapter);
        mDetailsAdapter.addData(mActionDatas);

        // 设置二级
        mProgressBar.setSecondaryProgress(100);
        mProgressBar.setBackgroundColor(0x00000000);
    }

    private void initData() {
        // 计算正确动作的个数
        int correctCount = 0;
        for (NewActionData actionData : mActionDatas) {
            if (actionData.getActionFlag() == 1) {
                correctCount++;
            }
        }
        setBatteryCount(correctCount + " 个");

        // 正确的比例
        int correctProportion = (int) ((float) correctCount / mActionDatas.size() * 100);
        mTvCorrectProportion.setText(correctProportion + "%");
        mTvErrorProportion.setText((100 - correctProportion) + "%");

        mProgressBar.setProgress(correctProportion);
        mTvErrorActionCount.setText("不规范动作 " + (mActionDatas.size() - correctCount) + " 个");
    }

    private void setBatteryCount(String text) {
        // 第一行
        SpannableString msp = new SpannableString(text);
        msp.setSpan(new RelativeSizeSpan(2.6f), 0, text.length() - 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mTvSecond.setText(msp);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void onEventMainThread(NewActionData event) {
        mActionDatas.add(event);
        initData();
        mDetailsAdapter.addData(0, event);
        mRecyclerView.scrollToPosition(0);
    }
}
